package com.utcs.modules.resource.basic.controller;

import com.utcs.common.persistence.Page;
import com.utcs.common.web.BaseController;
import com.utcs.modules.resource.basic.entity.User;
import com.utcs.modules.resource.basic.service.SystemService;
import com.utcs.modules.resource.basic.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * Created by Fly on 2017/5/23.
 */
@Controller
@RequestMapping(value = "resource/basic/user/")
public class UserController extends BaseController{

    @Autowired
    private SystemService systemService;
    @ModelAttribute("user")
    public User get(@RequestParam(required=false) Integer id) {
        if (id!=null){
            return UserUtils.get(id);
        }else{
            return new User();
        }
    }

    @RequiresPermissions("basic:user:view")
    @RequestMapping(value = {"/list",""})
    public String list(User user, Model model, HttpServletRequest request, HttpServletResponse response){
        Page<User> page = systemService.findUser(new Page<User>(request,response),user);
        model.addAttribute("page",page);
        return "modules/resource/basic/user";
    }


    @RequiresPermissions("basic:department:edit")
    @RequestMapping(value = "/save")
    public String save(User user, Model model, RedirectAttributes redirectAttributes) {
        logger.debug("user:" + user);
        return "redirect:/resource/basic/department/list";
    }
}
